import {
	getUrl
} from '@/scripts/compatible/getUrl.js'
import _objectSpread2 from '@/scripts/helpers/objectSpread2.js'

const get = (path, query = {}, config = {}) => {
	const url = getUrl(path)
	const token = uni.getStorageSync('token')
	const code = uni.getStorageSync('code')
	const header = _objectSpread2({
		"Content-UserToken": token,
		"content-Sign": code
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: 'GET',
			data: query,
			dataType: 'json',
			header,
			success: (data) => {
				resolve(data)
			},
			fail: () => {
				reject()
			},
			...config,
		})
	})
}

const post = (path, params = {}, config = {}) => {
	const url = getUrl(path)
	const token = uni.getStorageSync('token')
	const code = uni.getStorageSync('code')
	const header = _objectSpread2({
		"Content-UserToken": token,
		"content-Sign": code
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: 'POST',
			data: params,
			dataType: 'json',
			header,
			success: (data) => {
				resolve(data)
			},
			fail: () => {
				reject()
			},
			...config,
		})
	})
}

module.exports = {
	get,
	post,
}